<?php

session_start();

header('Access-Control-Allow-Origin:*');

date_default_timezone_set("PRC");

header("Content-Type: text/html;charset=utf8");

require_once '../common/in_mongo.php';
require_once '../db/tiku.db.php';

$action = filter_input(INPUT_POST, "action") ? filter_input(INPUT_POST, "action") : filter_input(INPUT_GET, "action");

$res = array();
switch ($action) {
    case 'uploadTiku':
        $res = uploadTiku();
        break;
    default :
}
echo json_encode($res);

function uploadTiku() {
    if (isset($_FILES['tiku'])) {
        $file = $_FILES['tiku'];
    }

    $content = file_get_contents($file['tmp_name']);

    $json = json_decode($content);

    if ($json) {
        
    } else {
        $res['code'] = 601;
        $res['msg'] = "json文件转换错误";
        return $res;
    }

    $count = count($json);

    if ($count > 0) {
        
    } else {
        $res['code'] = 602;
        $res['msg'] = "没有转换出有效数据";
        return $res;
    }

    $tikuDb = new TikuDb();
    if ($tikuDb->updateTiku($json)) {
        $res['code'] = 0;
        $res['msg'] = "";
    } else {
        $res['code'] = 603;
        $res['msg'] = "更新题库失败";
    }
    return $res;
}
